package com.zhan.drugStore.config;

import com.baomidou.mybatisplus.autoconfigure.MybatisPlusPropertiesCustomizer;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.handlers.MybatisEnumTypeHandler;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.zhan.drugStore.bean.bo.UserBO;
import com.zhan.drugStore.constant.DbConstant;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.shiro.SecurityUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.time.LocalDateTime;
import java.util.Optional;

/**
 * mybatis 配置类
 *
 * @author zhongfeng
 * @since 2010/11/06
 */
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {

    /*
     * 分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor interceptor = new PaginationInterceptor();
        //设置最大单页限制数量
        //interceptor.setLimit(AppConstant.GLOBAL_DATA_MAX_LIMIT);
        return interceptor;
    }


    /**
     * mybaits-plus 字段自动填充
     *
     * @return MetaObjectHandler
     */
    @Bean
    public MetaObjectHandler myMetaObjectHandler() {
        return new MetaObjectHandler() {
            @Override
            public void insertFill(MetaObject metaObject) {
                UserBO user = (UserBO) SecurityUtils.getSubject().getPrincipal();
                Long createBy = Optional.ofNullable(user != null ? user.getId() : null).orElse(1L);
                setInsertFieldValByName(DbConstant.FIELD_CREATE_BY.getValue(), createBy, metaObject);
                setInsertFieldValByName(DbConstant.FIELD_CREATE_DATE.getValue(), LocalDateTime.now(), metaObject);
                setInsertFieldValByName(DbConstant.FIELD_UPDATE_BY.getValue(), createBy, metaObject);
                setInsertFieldValByName(DbConstant.FIELD_UPDATE_DATE.getValue(), LocalDateTime.now(), metaObject);
            }

            @Override
            public void updateFill(MetaObject metaObject) {
                UserBO user = (UserBO) SecurityUtils.getSubject().getPrincipal();
                Long updateBy = Optional.ofNullable(user != null ? user.getId() : null).orElse(1L);
                setUpdateFieldValByName(DbConstant.FIELD_UPDATE_BY.getValue(), updateBy, metaObject);
                setUpdateFieldValByName(DbConstant.FIELD_UPDATE_DATE.getValue(), LocalDateTime.now(), metaObject);
            }
        };
    }

    @Bean
    public MybatisPlusPropertiesCustomizer mybatisPlusPropertiesCustomizer() {
        return properties -> {
            GlobalConfig globalConfig = properties.getGlobalConfig();
            globalConfig.setBanner(false);
            MybatisConfiguration configuration = new MybatisConfiguration();
            configuration.setDefaultEnumTypeHandler(MybatisEnumTypeHandler.class);
            properties.setConfiguration(configuration);
        };
    }
}
